//
//  TJRefreshController.m
//  CrossBorderPayment
//
//  Created by hc-101 on 2020/6/9.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJRefreshController.h"
#import "UIScrollView+EmptyDataSet.h"


@interface TJRefreshController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

@end

@implementation TJRefreshController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.emptyText = @"暂无数据".localizedString;
    self.emptyImageName = @"mXiaoxi";
    self.allowEmptyScroll = NO;
    if (self.hiddenNavigationBar == NO) {
        self.verticalOffset = -30;
    }
}

- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
         _collectionView.backgroundColor = [UIColor tj_backgroundColor];
         _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.emptyDataSetSource = self;
        _collectionView.emptyDataSetDelegate = self;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.tableViewStyle];
        tableView.separatorColor = [UIColor tj_separatorColor];
        tableView.backgroundColor = [UIColor tj_backgroundColor];
        tableView.tableFooterView = [[UIView alloc] init];
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.emptyDataSetSource = self;
        tableView.emptyDataSetDelegate = self;
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            tableView.estimatedSectionHeaderHeight = 0;
            tableView.estimatedSectionFooterHeight = 0;
            tableView.estimatedRowHeight = 0;
        }
        [self.view addSubview:tableView];
        _tableView = tableView;
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (nullable UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:self.emptyImageName];
}

- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return self.displayEmpty;
}

- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIColor clearColor];
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return _verticalOffset;
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    return 18;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return _allowEmptyScroll;
}

- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSString *text = self.emptyText;
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:text];
    [attStr addAttribute:NSFontAttributeName
                   value:[UIFont systemFontOfSize:13]
                   range:NSMakeRange(0, text.length)];
    [attStr addAttribute:NSForegroundColorAttributeName
                   value:UIColor_Hex(0x999999)
                   range:NSMakeRange(0, text.length)];
    return attStr;
}


- (TJRefreshFooter *)refreshFooter {
    if (!_refreshFooter) {
        TJRefreshFooter *refreshAutoNormalFooter = [TJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        refreshAutoNormalFooter.hidden = YES;
        refreshAutoNormalFooter.loadingView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
        _refreshFooter = refreshAutoNormalFooter;
    }
    return _refreshFooter;
}

- (TJRefreshHeader *)refreshHeader {
    if (!_refreshHeader) {
        TJRefreshHeader *refreshHeader = [TJRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        _refreshHeader = refreshHeader;
    }
    return _refreshHeader;
}

- (void)loadNewData {}
- (void)loadMoreData {}



@end
